package com.atguigu.gmall.user.service.impl;

import com.atguigu.gmall.model.user.UserAddress;
import com.atguigu.gmall.model.user.UserInfo;
import com.atguigu.gmall.user.mapper.UserAddressMapper;
import com.atguigu.gmall.user.mapper.UserInfoMapper;
import com.atguigu.gmall.user.service.UserInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.security.MessageDigest;
import java.util.List;


@Service
public class UserInfoServiceImpl implements UserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;
    //校验用户名及密码
    @Override
    public UserInfo login(UserInfo userInfo) {
        //1:当前用户是否存在
        UserInfo userInfo1 = userInfoMapper.selectOne(new QueryWrapper<UserInfo>().
                eq("login_name", userInfo.getLoginName()));
        if(null != userInfo1){
            //密码   加盐
            if(DigestUtils.md5DigestAsHex(userInfo.getPasswd().getBytes()).
                    equals(userInfo1.getPasswd())){
                return userInfo1;
            }
        }
        return null;
    }

    @Autowired
    private UserAddressMapper userAddressMapper;
    //  //内部资源 查询用户地址集合
    @Override
    public List<UserAddress> getUserAddressList(String userId) {
        return userAddressMapper.selectList(
                new QueryWrapper<UserAddress>().eq("user_id",userId));
    }

    public static void main(String[] args) {
        String s = DigestUtils.md5DigestAsHex("123".getBytes());
        System.out.println(s);

    }


}
